package no.uib.kwikktaim.core;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import javax.swing.DefaultListModel;

/**
 * This class saves/loads the playlist to a file using serialization.
 *
 */
public class SaveInfoToFile {
	File fileName;

	/**
	 * This method saves the model to a file. The filename is spesified by the "fileName"-variable.
	 * @return
	 */
	public static boolean saveToFile(SerializingBean bean, String fileName) {
		try {
			ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fileName));
			oos.writeObject(bean);
			return true;
		}
		catch(IOException e) {
			
		}
		return false;
	}

	/**
	 * This method retrieves the model from the file. If something goes wrong, null is returned.
	 * @return
	 */
	public static SerializingBean loadFromFile(String fileName) {
		try {
			ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fileName));
			return (SerializingBean) ois.readObject();
		}
		catch(Exception u) {
			return new SerializingBean();

		}
	}

}
